Add db.UpdateableByObject(Type)

This commit is contained in:
sunkaixuan 2023-12-27 22:44:42 +08:00
parent 6ed536063d
commit f201d91f27
4 changed files with 106 additions and 1 deletions

View File

@ -939,6 +939,21 @@ namespace SqlSugar
return result;
}
}
public UpdateExpressionMethodInfo UpdateableByObject(Type entityType)
{
UpdateExpressionMethodInfo reslut = new UpdateExpressionMethodInfo();
var methods = this.Context.GetType().GetMethods()
.Where(it => it.Name == "Updateable")
.Where(it => it.GetGenericArguments().Any())
.Where(it => !it.GetParameters().Any())
.Where(it => it.Name == "Updateable").ToList();
var method = methods.Single().MakeGenericMethod(entityType);
reslut.Context = this.Context;
reslut.MethodInfo = method;
reslut.Type = entityType;
reslut.objectValue = method.Invoke(Context, new object[] { });
return reslut;
}
public virtual IUpdateable<T> Updateable<T>(T[] UpdateObjs) where T : class, new()
{
InitMappingInfo<T>();

View File

@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
namespace SqlSugar
{
public class UpdateExpressionMethodInfo
{
internal SqlSugarProvider Context { get; set; }
internal MethodInfo MethodInfo { get; set; }
internal object objectValue { get; set; }
internal Type Type { get; set; }
public int ExecuteCommand()
{
if (Context == null) return 0;
var result = objectValue.GetType().GetMethod("ExecuteCommand").Invoke(objectValue, new object[] { });
return (int)result;
}
public async Task<int> ExecuteCommandAsync()
{
if (Context == null) return 0;
var result = objectValue.GetType().GetMyMethod("ExecuteCommandAsync", 0).Invoke(objectValue, new object[] { });
return await (Task<int>)result;
}
public UpdateExpressionMethodInfo Where(string expShortName,FormattableString whereExpressionString)
{
var newMethod = objectValue.GetType().GetMyMethod("Where", 1);
var exp = DynamicCoreHelper.GetWhere(Type, expShortName, whereExpressionString);
var result = newMethod.Invoke(objectValue, new object[] { exp });
return new UpdateExpressionMethodInfo()
{
objectValue = result,
Type=this.Type,
Context=this.Context
};
}
public UpdateExpressionMethodInfo SetColumns(string expShortName, FormattableString fieldExpressionString)
{
var newMethod = objectValue.GetType().GetMethods()
.Where(it=>
{
var result= it.Name == "SetColumns" && it.GetParameters().Count() == 1;
if (result)
{
return it.GetParameters().First().ToString().Contains(",System.Boolean");
}
return false;
})
.Single();
var exp1 =DynamicCoreHelper.GetWhere(Type, expShortName, fieldExpressionString);
var result = newMethod.Invoke(objectValue, new object[] { exp1 });
return new UpdateExpressionMethodInfo()
{
objectValue = result,
Type = this.Type,
Context = this.Context
};
}
}
}

View File

@ -15,7 +15,7 @@ namespace SqlSugar
}
public static LambdaExpression GetWhere(Type entityType, string shortName, FormattableString whereSql)
{
var parameter = Expression.Parameter(entityType, "it");
var parameter = Expression.Parameter(entityType, shortName);
// 提取 FormattableString 中的参数值
var arguments = whereSql.GetArguments();
@ -33,6 +33,26 @@ namespace SqlSugar
return lambda;
}
public static LambdaExpression GetObject(Type entityType, string shortName, FormattableString whereSql)
{
var parameter = Expression.Parameter(entityType, shortName);
// 提取 FormattableString 中的参数值
var arguments = whereSql.GetArguments();
var sql = ReplaceFormatParameters(whereSql.Format);
// 构建动态表达式,使用常量表达式和 whereSql 中的参数值
var lambda = SqlSugarDynamicExpressionParser.ParseLambda(
new[] { parameter },
typeof(object),
sql,
whereSql.GetArguments()
);
return lambda;
}
public static LambdaExpression GetWhere(Dictionary<string, Type> parameterDictionary, FormattableString whereSql)
{
var parameters = parameterDictionary.Select(it => Expression.Parameter(it.Value, it.Key)).ToArray();

View File

@ -723,6 +723,10 @@ namespace SqlSugar
{
return this.Context.UpdateableByObject(singleEntityObjectOrListObject);
}
public UpdateExpressionMethodInfo UpdateableByObject(Type entityType)
{
return this.Context.UpdateableByObject(entityType);
}
public IUpdateable<T> Updateable<T>() where T : class, new()
{
return this.Context.Updateable<T>();