Browse Source

Add :

db.DeleteableByObject
db.UpdateableByObject
pull/30/head
sunkaixuan 2 years ago
parent
commit
ad8851d19d
  1. 23
      Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteMethodInfo.cs
  2. 92
      Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs
  3. 8
      Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs
  4. 23
      Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateMethodInfo.cs
  5. 2
      Src/Asp.Net/SqlSugar/SqlSugar.csproj
  6. 8
      Src/Asp.Net/SqlSugar/SqlSugarClient.cs
  7. 9
      Src/Asp.Net/SqlSugar/SqlSugarScope.cs

23
Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteMethodInfo.cs

@ -0,0 +1,23 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace SqlSugar
{
public class DeleteMethodInfo
{
internal SqlSugarProvider Context { get; set; }
internal MethodInfo MethodInfo { get; set; }
internal object objectValue { get; set; }
public int ExecuteCommand()
{
if (Context == null) return 0;
var inertable=MethodInfo.Invoke(Context, new object[] { objectValue });
var result= inertable.GetType().GetMethod("ExecuteCommand").Invoke(inertable,new object[] { });
return (int)result;
}
}
}

92
Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs

@ -767,6 +767,52 @@ namespace SqlSugar
#endregion
#region Deleteable
public DeleteMethodInfo DeleteableByObject(object singleEntityObjectOrListObject)
{
if (singleEntityObjectOrListObject == null)
return new DeleteMethodInfo();
if (singleEntityObjectOrListObject.GetType().FullName.IsCollectionsList())
{
var list = ((IList)singleEntityObjectOrListObject);
if (list == null || list.Count == 0)
return new DeleteMethodInfo();
var type = list[0].GetType();
var newList = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
foreach (var item in list)
{
newList.Add(item);
}
var methods = this.Context.GetType().GetMethods()
.Where(it => it.Name == "Deleteable")
.Where(it => it.GetGenericArguments().Any())
.Where(it => it.GetParameters().Any(z => z.ParameterType.Name.StartsWith("List")))
.Where(it => it.Name == "Deleteable").ToList();
var method = methods.Single().MakeGenericMethod(newList.GetType().GetGenericArguments().FirstOrDefault());
DeleteMethodInfo result = new DeleteMethodInfo()
{
Context = this.Context,
MethodInfo = method,
objectValue = newList
};
return result;
}
else
{
var methods = this.Context.GetType().GetMethods()
.Where(it => it.Name == "Deleteable")
.Where(it => it.GetGenericArguments().Any())
.Where(it => it.GetParameters().Any(z => z.ParameterType.Name == "T"))
.Where(it => it.Name == "Deleteable").ToList();
var method = methods.Single().MakeGenericMethod(singleEntityObjectOrListObject.GetType());
DeleteMethodInfo result = new DeleteMethodInfo()
{
Context = this.Context,
MethodInfo = method,
objectValue = singleEntityObjectOrListObject
};
return result;
}
}
public virtual IDeleteable<T> Deleteable<T>() where T : class, new()
{
InitMappingInfo<T>();
@ -806,6 +852,52 @@ namespace SqlSugar
#endregion
#region Updateable
public UpdateMethodInfo UpdateableByObject(object singleEntityObjectOrListObject)
{
if (singleEntityObjectOrListObject == null)
return new UpdateMethodInfo();
if (singleEntityObjectOrListObject.GetType().FullName.IsCollectionsList())
{
var list = ((IList)singleEntityObjectOrListObject);
if (list == null || list.Count == 0)
return new UpdateMethodInfo();
var type = list[0].GetType();
var newList = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
foreach (var item in list)
{
newList.Add(item);
}
var methods = this.Context.GetType().GetMethods()
.Where(it => it.Name == "Updateable")
.Where(it => it.GetGenericArguments().Any())
.Where(it => it.GetParameters().Any(z => z.ParameterType.Name.StartsWith("List")))
.Where(it => it.Name == "Updateable").ToList();
var method = methods.Single().MakeGenericMethod(newList.GetType().GetGenericArguments().First());
UpdateMethodInfo result = new UpdateMethodInfo()
{
Context = this.Context,
MethodInfo = method,
objectValue = newList
};
return result;
}
else
{
var methods = this.Context.GetType().GetMethods()
.Where(it => it.Name == "Updateable")
.Where(it => it.GetGenericArguments().Any())
.Where(it => it.GetParameters().Any(z => z.ParameterType.Name == "T"))
.Where(it => it.Name == "Updateable").ToList();
var method = methods.Single().MakeGenericMethod(singleEntityObjectOrListObject.GetType());
UpdateMethodInfo result = new UpdateMethodInfo()
{
Context = this.Context,
MethodInfo = method,
objectValue = singleEntityObjectOrListObject
};
return result;
}
}
public virtual IUpdateable<T> Updateable<T>(T[] UpdateObjs) where T : class, new()
{
InitMappingInfo<T>();

8
Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs

@ -144,6 +144,10 @@ namespace SqlSugar
{
ScopedContext.Close();
}
public DeleteMethodInfo DeleteableByObject(object singleEntityObjectOrListObject)
{
return ScopedContext.DeleteableByObject(singleEntityObjectOrListObject);
}
public IDeleteable<T> Deleteable<T>() where T : class, new()
{
return ScopedContext.Deleteable<T>();
@ -615,6 +619,10 @@ namespace SqlSugar
{
return ScopedContext.UnionAll(queryables);
}
public UpdateMethodInfo UpdateableByObject(object singleEntityObjectOrListObject)
{
return ScopedContext.UpdateableByObject(singleEntityObjectOrListObject);
}
public IUpdateable<T> Updateable<T>() where T : class, new()
{

23
Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateMethodInfo.cs

@ -0,0 +1,23 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace SqlSugar
{
public class UpdateMethodInfo
{
internal SqlSugarProvider Context { get; set; }
internal MethodInfo MethodInfo { get; set; }
internal object objectValue { get; set; }
public int ExecuteCommand()
{
if (Context == null) return 0;
var inertable=MethodInfo.Invoke(Context, new object[] { objectValue });
var result= inertable.GetType().GetMethod("ExecuteCommand").Invoke(inertable,new object[] { });
return (int)result;
}
}
}

2
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -133,6 +133,8 @@
<Compile Include="Abstract\SaveableProvider\StorageableDataTable.cs" />
<Compile Include="Abstract\SqlBuilderProvider\SqlBuilderProvider_Condition.cs" />
<Compile Include="Abstract\SugarProvider\SqlSugarCoreProvider.cs" />
<Compile Include="Abstract\DeleteProvider\DeleteMethodInfo.cs" />
<Compile Include="Abstract\UpdateProvider\UpdateMethodInfo.cs" />
<Compile Include="Abstract\UpdateProvider\UpdateableHelper.cs" />
<Compile Include="Abstract\UpdateProvider\SplitTableUpdateByObjectProvider.cs" />
<Compile Include="Entities\DbFastestProperties.cs" />

8
Src/Asp.Net/SqlSugar/SqlSugarClient.cs

@ -665,6 +665,10 @@ namespace SqlSugar
#endregion
#region Updateable
public UpdateMethodInfo UpdateableByObject(object singleEntityObjectOrListObject)
{
return this.Context.UpdateableByObject(singleEntityObjectOrListObject);
}
public IUpdateable<T> Updateable<T>() where T : class, new()
{
return this.Context.Updateable<T>();
@ -713,6 +717,10 @@ namespace SqlSugar
#endregion
#region Deleteable
public DeleteMethodInfo DeleteableByObject(object singleEntityObjectOrListObject)
{
return this.Context.DeleteableByObject(singleEntityObjectOrListObject);
}
public IDeleteable<T> Deleteable<T>() where T : class, new()
{
return this.Context.Deleteable<T>();

9
Src/Asp.Net/SqlSugar/SqlSugarScope.cs

@ -120,6 +120,10 @@ namespace SqlSugar
{
return ScopedContext.CommitTranAsync();
}
public DeleteMethodInfo DeleteableByObject(object singleEntityObjectOrListObject)
{
return ScopedContext.DeleteableByObject(singleEntityObjectOrListObject);
}
public IDeleteable<T> Deleteable<T>() where T : class, new()
{
return ScopedContext.Deleteable<T>();
@ -626,7 +630,10 @@ namespace SqlSugar
{
return ScopedContext.UnionAll(queryables);
}
public UpdateMethodInfo UpdateableByObject(object singleEntityObjectOrListObject)
{
return ScopedContext.UpdateableByObject(singleEntityObjectOrListObject);
}
public IUpdateable<T> Updateable<T>() where T : class, new()
{
return ScopedContext.Updateable<T>();

Loading…
Cancel
Save