Synchronization code

This commit is contained in:
sunkaixuan 2022-12-15 17:33:57 +08:00
parent 5ff7554497
commit 37adfd52ee
6 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class StorageableMethodInfo
{
internal SqlSugarProvider Context { get; set; }
internal MethodInfo MethodInfo { get; set; }
internal object objectValue { get; set; }
public int ExecuteCommand()
{
if (Context == null) return 0;
object objectValue = null;
MethodInfo method = GetSaveMethod(ref objectValue);
return (int)method.Invoke(objectValue, new object[] { });
}
public StorageableAsMethodInfo AsInsertable
{
get
{
var type = "AsInsertable";
return GetAs(type);
}
}
public StorageableAsMethodInfo AsUpdateable
{
get
{
var type = "AsUpdateable";
return GetAs(type);
}
}
private StorageableAsMethodInfo GetAs(string type)
{
object objectValue = null;
MethodInfo method = GetSaveMethod(ref objectValue);
method = objectValue.GetType().GetMethod("ToStorage");
objectValue = method.Invoke(objectValue, new object[] { });
StorageableAsMethodInfo result = new StorageableAsMethodInfo(type);
result.ObjectValue = objectValue;
result.Method = method;
return result;
}
private MethodInfo GetSaveMethod(ref object callValue)
{
callValue = MethodInfo.Invoke(Context, new object[] { objectValue });
return callValue.GetType().GetMethod("ExecuteCommand");
}
public StorageableMethodInfo ToStorage()
{
return this;
}
}
public class StorageableAsMethodInfo
{
private StorageableAsMethodInfo() { }
private string type;
public StorageableAsMethodInfo(string type)
{
this.type = type;
}
internal object ObjectValue { get; set; }
internal MethodInfo Method { get; set; }
public int ExecuteCommand()
{
PropertyInfo property = ObjectValue.GetType().GetProperty(type);
var value = property.GetValue(ObjectValue);
var newObj= value.GetType().GetMethod("ExecuteCommand").Invoke(value, new object[] { });
return (int)newObj;
}
}
}

View File

@ -1,9 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@ -869,6 +871,52 @@ namespace SqlSugar
data.Columns.Add(new DataColumn("SugarColumns", typeof(string[])));
return result;
}
public StorageableMethodInfo StorageableByObject(object singleEntityObjectOrList)
{
if (singleEntityObjectOrList == null)
return new StorageableMethodInfo();
if (singleEntityObjectOrList.GetType().FullName.IsCollectionsList())
{
var list = ((IList)singleEntityObjectOrList);
if(list==null|| list.Count==0)
return new StorageableMethodInfo();
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 == "Storageable")
.Where(it => it.GetGenericArguments().Any())
.Where(it => it.GetParameters().Any(z => z.ParameterType.Name.StartsWith("List")))
.Where(it => it.Name == "Storageable").ToList();
var method = methods.Single().MakeGenericMethod(newList.GetType().GetGenericArguments().First());
StorageableMethodInfo result = new StorageableMethodInfo()
{
Context = this.Context,
MethodInfo = method,
objectValue = newList
};
return result;
}
else
{
var methods = this.Context.GetType().GetMethods()
.Where(it => it.Name == "Storageable")
.Where(it => it.GetGenericArguments().Any())
.Where(it => it.GetParameters().Any(z => z.ParameterType.Name == "T"))
.Where(it => it.Name == "Storageable").ToList();
var method = methods.Single().MakeGenericMethod(singleEntityObjectOrList.GetType());
StorageableMethodInfo result = new StorageableMethodInfo()
{
Context = this.Context,
MethodInfo = method,
objectValue = singleEntityObjectOrList
};
return result;
}
}
#endregion
#region Reportable

View File

@ -588,6 +588,10 @@ namespace SqlSugar
{
return ScopedContext.Storageable(data);
}
public StorageableMethodInfo StorageableByObject(object singleEntityObjectOrListObject)
{
return ScopedContext.StorageableByObject(singleEntityObjectOrListObject);
}
public ISugarQueryable<T> Union<T>(List<ISugarQueryable<T>> queryables) where T : class, new()
{

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
namespace SqlSugar
@ -146,6 +147,9 @@ namespace SqlSugar
ISaveable<T> Saveable<T>(List<T> saveObjects) where T : class, new();
[Obsolete("use Storageable")]
ISaveable<T> Saveable<T>(T saveObject) where T : class, new();
StorageableMethodInfo StorageableByObject(object singleEntityObjectOrListObject);
#endregion
#region Queue

View File

@ -538,6 +538,10 @@ namespace SqlSugar
{
return this.Context.Saveable(saveObject);
}
public StorageableMethodInfo StorageableByObject(object singleEntityObjectOrListObject)
{
return this.Context.StorageableByObject(singleEntityObjectOrListObject);
}
#endregion
#region Reportable

View File

@ -599,6 +599,10 @@ namespace SqlSugar
{
return ScopedContext.Storageable(data);
}
public StorageableMethodInfo StorageableByObject(object singleEntityObjectOrListObject)
{
return this.ScopedContext.StorageableByObject(singleEntityObjectOrListObject);
}
public ISugarQueryable<T> Union<T>(List<ISugarQueryable<T>> queryables) where T : class, new()
{