mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-23 20:43:46 +08:00
Synchronization code
This commit is contained in:
@@ -4,6 +4,9 @@ using System.Data;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public partial class FastestProvider<T>:IFastest<T> where T:class,new()
|
public partial class FastestProvider<T>:IFastest<T> where T:class,new()
|
||||||
@@ -179,6 +182,22 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return BulkMergeAsync(datas).GetAwaiter().GetResult();
|
return BulkMergeAsync(datas).GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
|
public int BulkMerge(DataTable dataTable, string[] whereColumns,bool isIdentity)
|
||||||
|
{
|
||||||
|
object newValue, fastestMethod;
|
||||||
|
MethodInfo bulkCopyMethod;
|
||||||
|
_BulkMerge(dataTable, whereColumns, out newValue, out fastestMethod, out bulkCopyMethod,false,isIdentity);
|
||||||
|
var result = (int)bulkCopyMethod.Invoke(fastestMethod, new object[] { newValue });
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public Task<int> BulkMergeAsync(DataTable dataTable, string[] whereColumns, bool isIdentity)
|
||||||
|
{
|
||||||
|
object newValue, fastestMethod;
|
||||||
|
MethodInfo bulkCopyMethod;
|
||||||
|
_BulkMerge(dataTable, whereColumns, out newValue, out fastestMethod, out bulkCopyMethod,true,isIdentity);
|
||||||
|
var result = (Task<int>)bulkCopyMethod.Invoke(fastestMethod, new object[] { newValue });
|
||||||
|
return result;
|
||||||
|
}
|
||||||
public Task<int> BulkMergeAsync(List<T> datas, string[] whereColumns)
|
public Task<int> BulkMergeAsync(List<T> datas, string[] whereColumns)
|
||||||
{
|
{
|
||||||
var updateColumns = entityInfo.Columns.Where(it => !it.IsPrimarykey && !it.IsIdentity && !it.IsOnlyIgnoreUpdate && !it.IsIgnore).Select(it => it.DbColumnName ?? it.PropertyName).ToArray();
|
var updateColumns = entityInfo.Columns.Where(it => !it.IsPrimarykey && !it.IsIdentity && !it.IsOnlyIgnoreUpdate && !it.IsIgnore).Select(it => it.DbColumnName ?? it.PropertyName).ToArray();
|
||||||
@@ -247,6 +266,35 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Core
|
#region Core
|
||||||
|
private void _BulkMerge(DataTable dataTable, string[] whereColumns, out object newValue, out object fastestMethod, out MethodInfo bulkCopyMethod,bool isAsync, bool isIdentity)
|
||||||
|
{
|
||||||
|
Check.ExceptionEasy(this.AsName.IsNullOrEmpty(), "need .AS(tablaeName) ", "需要 .AS(tablaeName) 设置表名");
|
||||||
|
var className = "BulkMerge_" +isIdentity+ this.AsName.GetNonNegativeHashCodeString();
|
||||||
|
var builder = this.context.DynamicBuilder().CreateClass(className, new SugarTable()
|
||||||
|
{
|
||||||
|
TableName = this.AsName
|
||||||
|
});
|
||||||
|
foreach (DataColumn item in dataTable.Columns)
|
||||||
|
{
|
||||||
|
var isPrimaryKey = whereColumns.Any(it => it.EqualCase(item.ColumnName));
|
||||||
|
builder.CreateProperty(item.ColumnName, item.DataType, new SugarColumn()
|
||||||
|
{
|
||||||
|
IsPrimaryKey = isPrimaryKey,
|
||||||
|
IsIdentity=isIdentity&& isPrimaryKey
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
var dicList = this.context.Utilities.DataTableToDictionaryList(dataTable);
|
||||||
|
var type = builder.WithCache().BuilderType();
|
||||||
|
var value = this.context.DynamicBuilder().CreateObjectByType(type, dicList);
|
||||||
|
newValue = UtilMethods.ConvertToObjectList(type, value);
|
||||||
|
fastestMethod = this.context.GetType()
|
||||||
|
.GetMethod("Fastest")
|
||||||
|
.MakeGenericMethod(type)
|
||||||
|
.Invoke(this.context, null);
|
||||||
|
bulkCopyMethod = fastestMethod.GetType().GetMyMethod(isAsync? "BulkMergeAsync" : "BulkMerge", 1);
|
||||||
|
}
|
||||||
|
|
||||||
private async Task<int> _BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns)
|
private async Task<int> _BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@@ -36,6 +36,8 @@ namespace SqlSugar
|
|||||||
SplitFastest<T> SplitTable();
|
SplitFastest<T> SplitTable();
|
||||||
Task<int> BulkMergeAsync(List<T> datas);
|
Task<int> BulkMergeAsync(List<T> datas);
|
||||||
int BulkMerge(List<T> datas);
|
int BulkMerge(List<T> datas);
|
||||||
|
int BulkMerge(DataTable dataTable, string[] whereColumns,bool isIdentity);
|
||||||
|
Task<int> BulkMergeAsync(DataTable dataTable, string[] whereColumns, bool isIdentity);
|
||||||
Task<int> BulkMergeAsync(List<T> datas, string[] whereColumns);
|
Task<int> BulkMergeAsync(List<T> datas, string[] whereColumns);
|
||||||
int BulkMerge(List<T> datas, string[] whereColumns);
|
int BulkMerge(List<T> datas, string[] whereColumns);
|
||||||
Task<int> BulkMergeAsync(List<T> datas, string[] whereColumns, string[] updateColumns);
|
Task<int> BulkMergeAsync(List<T> datas, string[] whereColumns, string[] updateColumns);
|
||||||
|
@@ -18,6 +18,18 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
public class UtilMethods
|
public class UtilMethods
|
||||||
{
|
{
|
||||||
|
public static object ConvertToObjectList(Type targetType, List<object> sourceList)
|
||||||
|
{
|
||||||
|
// 创建 List<Type> 类型的实例
|
||||||
|
object resultList = Activator.CreateInstance(typeof(List<>).MakeGenericType(targetType));
|
||||||
|
// 获取 Add 方法
|
||||||
|
var addMethod = resultList.GetType().GetMethod("Add");
|
||||||
|
foreach (var obj in sourceList)
|
||||||
|
{
|
||||||
|
addMethod.Invoke(resultList, new object[] { obj });
|
||||||
|
}
|
||||||
|
return resultList;
|
||||||
|
}
|
||||||
public static Dictionary<string, object> DataRowToDictionary(DataRow row)
|
public static Dictionary<string, object> DataRowToDictionary(DataRow row)
|
||||||
{
|
{
|
||||||
Dictionary<string, object> dictionary = new Dictionary<string, object>();
|
Dictionary<string, object> dictionary = new Dictionary<string, object>();
|
||||||
|
Reference in New Issue
Block a user