- 增加 DynamicFilter Custom 自定义解析;

This commit is contained in:
2881099
2021-12-09 00:21:20 +08:00
parent 59f14fcd13
commit 8a83fea60a
6 changed files with 155 additions and 15 deletions

View File

@ -544,17 +544,34 @@ namespace FreeSql.Internal.CommonProvider
{
if (string.IsNullOrEmpty(fi.Field) == false)
{
Expression exp = ConvertStringPropertyToExpression(fi.Field);
Expression exp = null;
switch (fi.Operator)
{
case DynamicFilterOperator.Custom:
var fiValueCustomArray = fi.Field?.ToString().Split(new[] { ' ' }, 2);
if (fiValueCustomArray.Length != 2) throw new ArgumentException("Custom 要求 Field 应该空格分割,并且长度为 2格式{静态方法名}{空格}{反射信息}");
if (string.IsNullOrWhiteSpace(fiValueCustomArray[0])) throw new ArgumentException("Custom {静态方法名}不能为空,格式:{静态方法名}{空格}{反射信息}");
if (string.IsNullOrWhiteSpace(fiValueCustomArray[1])) throw new ArgumentException("Custom {反射信息}不能为空,格式:{静态方法名}{空格}{反射信息}");
var fiValue1Type = Type.GetType(fiValueCustomArray[1]);
if (fiValue1Type == null) throw new ArgumentException($"Custom 找到对应的{{反射信息}}{fiValueCustomArray[1]}");
var fiValue0Method = fiValue1Type.GetMethod(fiValueCustomArray[0], new Type[] { typeof(string) });
if (fiValue0Method == null) throw new ArgumentException($"Custom 找到对应的{{静态方法名}}{fiValueCustomArray[0]}");
var fiValue0MethodReturn = fiValue0Method?.Invoke(null, new object[] { fi.Value?.ToString() })?.ToString();
exp = Expression.Call(typeof(SqlExt).GetMethod("InternalRawSql", BindingFlags.NonPublic | BindingFlags.Static), Expression.Constant(fiValue0MethodReturn, typeof(string)));
break;
case DynamicFilterOperator.Contains:
case DynamicFilterOperator.StartsWith:
case DynamicFilterOperator.EndsWith:
case DynamicFilterOperator.NotContains:
case DynamicFilterOperator.NotStartsWith:
case DynamicFilterOperator.NotEndsWith:
exp = ConvertStringPropertyToExpression(fi.Field);
if (exp.Type != typeof(string)) exp = Expression.TypeAs(exp, typeof(string));
break;
default:
exp = ConvertStringPropertyToExpression(fi.Field);
break;
}
switch (fi.Operator)
{
@ -579,7 +596,7 @@ namespace FreeSql.Internal.CommonProvider
if (fiValueRangeArray.Length != 2) throw new ArgumentException($"Range 要求 Value 应该逗号分割,并且长度为 2");
exp = Expression.AndAlso(
Expression.GreaterThanOrEqual(exp, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fiValueRangeArray[0]), exp.Type)),
Expression.LessThan(exp, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fiValueRangeArray[1]), exp.Type)));
Expression.LessThan(exp, Expression.Constant(Utils.GetDataReaderValue(exp.Type, fiValueRangeArray[1]), exp.Type)));
break;
case DynamicFilterOperator.DateRange:
var fiValueDateRangeArray = getFiListValue();

View File

@ -120,6 +120,21 @@ namespace FreeSql.Internal.Model
/// not in (1,2,3)<para></para>
/// 此时 Value 的值格式为逗号分割value1,value2,value3... 或者数组
/// </summary>
NotAny
NotAny,
/// <summary>
/// 自定义解析,此时 Field 为反射信息Value 为静态方法的参数(string)<para></para>
/// 示范:{ Operator: "Custom", Field: "RawSql webapp1.DynamicFilterCustom,webapp1", Value: "(id,name) in ((1,'k'),(2,'m'))" }<para></para>
/// 注意:使用者自己承担【注入风险】<para></para>
/// 静态方法定义示范:<para></para>
/// namespace webapp1<para></para>
/// {<para></para>
/// public class DynamicFilterCustom<para></para>
/// {<para></para>
/// public static string RawSql(string value) => value;<para></para>
/// }<para></para>
/// }<para></para>
/// </summary>
Custom
}
}