Synchronization code

This commit is contained in:
sunkaixuan
2023-09-10 20:24:18 +08:00
parent a6e3868dff
commit 06be7a786b
5 changed files with 87 additions and 0 deletions

View File

@@ -120,6 +120,11 @@ namespace SqlSugar
}
return this;
}
public QueryFilterProvider AddTableFilter(Type type,string shortName, FormattableString expString, FilterJoinPosition filterJoinType = FilterJoinPosition.On)
{
var exp = DynamicCoreHelper.GetWhere(type, shortName, expString);
return AddTableFilter(type, exp, filterJoinType);
}
public QueryFilterProvider AddTableFilter(Type type,Expression expression, FilterJoinPosition filterJoinType = FilterJoinPosition.On)
{
var isOn = filterJoinType == FilterJoinPosition.On;

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar
@@ -26,5 +27,6 @@ namespace SqlSugar
public static bool Check_StringIdentity = true;
public static Func<string,string> Check_FieldFunc;
public static Type DynamicExpressionParserType;
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
namespace SqlSugar
{
public class DynamicCoreHelper
{
public static LambdaExpression GetWhere(Type entityType, string shortName, FormattableString whereSql)
{
var parameter = Expression.Parameter(entityType, "it");
// 提取 FormattableString 中的参数值
var arguments = whereSql.GetArguments();
var sql = ReplaceFormatParameters(whereSql.Format);
// 构建动态表达式,使用常量表达式和 whereSql 中的参数值
var lambda = SqlSugarDynamicExpressionParser.ParseLambda(
new[] { parameter },
typeof(bool),
sql,
whereSql.GetArguments()
);
return lambda;
}
private static string ReplaceFormatParameters(string format)
{
int parameterIndex = 0; // 起始参数索引
return Regex.Replace(format, @"\{\d+\}", match =>
{
string replacement = $"@{parameterIndex}";
parameterIndex++;
return replacement;
});
}
}
}

View File

@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
namespace SqlSugar
{
public class SqlSugarDynamicExpressionParser
{
public static LambdaExpression ParseLambda(ParameterExpression[] parameterExpressions, Type type, string sql, object[] objects)
{
if (StaticConfig.DynamicExpressionParserType == null)
{
Check.ExceptionEasy("Please at program startup assignment: StaticConfig DynamicExpressionParserType = typeof (DynamicExpressionParser); NUGET is required to install Dynamic.Core", "请在程序启动时赋值: StaticConfig.DynamicExpressionParserType = typeof(DynamicExpressionParser); 需要NUGET安装 Dynamic.Core");
}
// 查找 ParseLambda 方法
MethodInfo parseLambdaMethod = StaticConfig.DynamicExpressionParserType
.GetMyMethod("ParseLambda",4, typeof(ParameterExpression[]), typeof(Type), typeof(string), typeof(object[]));
if (parseLambdaMethod == null)
{
throw new InvalidOperationException("ParseLambda method not found in DynamicExpressionParserType.");
}
// 调用 ParseLambda 方法来解析 Lambda 表达式
var lambda = (LambdaExpression)parseLambdaMethod.Invoke(null, new object[] { parameterExpressions, type, sql, objects });
return lambda;
}
}
}

View File

@@ -186,6 +186,8 @@
<Compile Include="ExpressionsToSql\Subquery\Items\SubEnableTableFilter.cs" />
<Compile Include="Interface\ICustomConditionalFunc.cs" />
<Compile Include="Interface\ISugarDataConverter.cs" />
<Compile Include="Json2Sql\DynamicLinq\DynamicCoreHelper.cs" />
<Compile Include="Json2Sql\DynamicLinq\SqlSugarDynamicExpressionParser.cs" />
<Compile Include="Json2Sql\Entities\JsonDeleteResult.cs" />
<Compile Include="Json2Sql\Entities\JsonInsertResult.cs" />
<Compile Include="Json2Sql\Entities\JsonQueryResult.cs" />