mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
- 增加 DynamicFilter Custom 自定义解析;
This commit is contained in:
@ -140,6 +140,17 @@ namespace FreeSql
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注意:使用者自己承担【注入风险】
|
||||
/// </summary>
|
||||
/// <param name="sql"></param>
|
||||
/// <returns></returns>
|
||||
static bool InternalRawSql([RawValue] string sql)
|
||||
{
|
||||
expContext.Value.Result = sql;
|
||||
return false;
|
||||
}
|
||||
|
||||
#region 大小判断
|
||||
/// <summary>
|
||||
/// 大于 >
|
||||
|
@ -1202,6 +1202,13 @@
|
||||
<param name="column"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.SqlExt.InternalRawSql(System.String)">
|
||||
<summary>
|
||||
注意:使用者自己承担【注入风险】
|
||||
</summary>
|
||||
<param name="sql"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.SqlExt.GreaterThan``1(``0,``0)">
|
||||
<summary>
|
||||
大于 >
|
||||
@ -4167,6 +4174,21 @@
|
||||
此时 Value 的值格式为逗号分割:value1,value2,value3... 或者数组
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:FreeSql.Internal.Model.DynamicFilterOperator.Custom">
|
||||
<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>
|
||||
</member>
|
||||
<member name="P:FreeSql.Internal.Model.FetchCallbackArgs`1.IsBreak">
|
||||
<summary>
|
||||
是否放弃继续读取
|
||||
|
@ -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();
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user