Synchronization code

This commit is contained in:
sunkaixuan 2023-09-20 23:04:30 +08:00
parent 4ae8364a37
commit 610ad226d6
7 changed files with 57 additions and 10 deletions

View File

@ -82,7 +82,12 @@ namespace SqlSugar
this.QueryableObj = method.Invoke(QueryableObj, new object[] { groupBySql });
return this;
}
public QueryMethodInfo Where(string expShortName, FormattableString expressionString)
{
var method = QueryableObj.GetType().GetMyMethod("Where", 2, typeof(string),typeof(FormattableString));
this.QueryableObj = method.Invoke(QueryableObj, new object[] { expShortName, expressionString });
return this;
}
public QueryMethodInfo Where(List<IConditionalModel> conditionalModels)
{
var method = QueryableObj.GetType().GetMyMethod("Where", 1, typeof(List<IConditionalModel>));

View File

@ -917,6 +917,12 @@ namespace SqlSugar
}
return this;
}
public virtual ISugarQueryable<T> Where(string expShortName, FormattableString expressionString)
{
var exp = DynamicCoreHelper.GetWhere<T>(expShortName, expressionString);
_Where(exp);
return this;
}
public virtual ISugarQueryable<T> Where(Expression<Func<T, bool>> expression)
{
this._Where(expression);

View File

@ -29,5 +29,6 @@ namespace SqlSugar
public static bool EnableAllWhereIF = false;
public static Func<string,string> Check_FieldFunc;
public static Type DynamicExpressionParserType;
public static object DynamicExpressionParsingConfig;
}
}

View File

@ -84,6 +84,7 @@ namespace SqlSugar
ISugarQueryable<T> WhereColumns(Dictionary<string, object> columns);
ISugarQueryable<T> TranLock(DbLockType? LockType = DbLockType.Wait);
ISugarQueryable<T> Where(Expression<Func<T, bool>> expression);
ISugarQueryable<T> Where(string expShortName, FormattableString expressionString);
ISugarQueryable<T> Where(string whereString, object parameters = null);
ISugarQueryable<T> Where(IFuncModel funcModel);
ISugarQueryable<T> Where(List<IConditionalModel> conditionalModels);

View File

@ -8,6 +8,10 @@ namespace SqlSugar
{
public class DynamicCoreHelper
{
public static Expression<Func<T, bool>> GetWhere<T>(string shortName, FormattableString whereSql)
{
return (Expression<Func<T, bool>>)GetWhere(typeof(T), shortName, whereSql);
}
public static LambdaExpression GetWhere(Type entityType, string shortName, FormattableString whereSql)
{
var parameter = Expression.Parameter(entityType, "it");

View File

@ -16,19 +16,39 @@ namespace SqlSugar
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)
if (StaticConfig.DynamicExpressionParsingConfig != null)
{
throw new InvalidOperationException("ParseLambda method not found in DynamicExpressionParserType.");
// 查找 ParseLambda 方法
MethodInfo parseLambdaMethod = StaticConfig.DynamicExpressionParserType
.GetMyMethod("ParseLambda", 5, StaticConfig.DynamicExpressionParsingConfig.GetType(), 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[] { StaticConfig.DynamicExpressionParsingConfig, parameterExpressions, type, sql, objects });
return lambda;
}
else
{
// 调用 ParseLambda 方法来解析 Lambda 表达式
var lambda = (LambdaExpression)parseLambdaMethod.Invoke(null, new object[] { parameterExpressions, type, sql, objects });
// 查找 ParseLambda 方法
MethodInfo parseLambdaMethod = StaticConfig.DynamicExpressionParserType
.GetMyMethod("ParseLambda",4, typeof(ParameterExpression[]), typeof(Type), typeof(string), typeof(object[]));
return lambda;
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

@ -72,6 +72,16 @@ namespace SqlSugar
it.GetParameters()[2].ParameterType == parameterType3&&
it.GetParameters()[3].ParameterType == parameterType4);
}
public static MethodInfo GetMyMethod(this Type type, string name, int argCount, Type parameterType, Type parameterType2, Type parameterType3, Type parameterType4, Type parameterType5)
{
return type.GetMethods().Where(it => it.Name == name).FirstOrDefault(it =>
it.GetParameters().Length == argCount &&
it.GetParameters().First().ParameterType == parameterType &&
it.GetParameters()[1].ParameterType == parameterType2 &&
it.GetParameters()[2].ParameterType == parameterType3 &&
it.GetParameters()[3].ParameterType == parameterType4&&
it.GetParameters()[4].ParameterType == parameterType5);
}
public static List<T> ToList<T>(this T thisValue,Func<T,T> action) where T:class,new()
{
return new List<T> { thisValue };