mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-23 04:23:47 +08:00
Add BulkMerge (userInfo, new string[] { "UserId" }, new string[] { "UserName" }, true)
This commit is contained in:
@@ -198,6 +198,18 @@ namespace SqlSugar
|
|||||||
var result = (Task<int>)bulkCopyMethod.Invoke(fastestMethod, new object[] { newValue });
|
var result = (Task<int>)bulkCopyMethod.Invoke(fastestMethod, new object[] { newValue });
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
public int BulkMerge(DataTable dataTable, string[] whereColumns, string[] updateColumns, bool isIdentity)
|
||||||
|
{
|
||||||
|
return BulkMergeAsync(dataTable, whereColumns, updateColumns, isIdentity).GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
public Task<int> BulkMergeAsync(DataTable dataTable, string[] whereColumns, string[] updateColumns, bool isIdentity)
|
||||||
|
{
|
||||||
|
object newValue, fastestMethod;
|
||||||
|
MethodInfo bulkCopyMethod;
|
||||||
|
_BulkMerge(dataTable, whereColumns,updateColumns, out newValue, out fastestMethod, out bulkCopyMethod, true, isIdentity);
|
||||||
|
var result = (Task<int>)bulkCopyMethod.Invoke(fastestMethod, new object[] { newValue,whereColumns,updateColumns });
|
||||||
|
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();
|
||||||
@@ -303,6 +315,40 @@ namespace SqlSugar
|
|||||||
.Invoke(this.context, null);
|
.Invoke(this.context, null);
|
||||||
bulkCopyMethod = fastestMethod.GetType().GetMyMethod(isAsync? "BulkMergeAsync" : "BulkMerge", 1);
|
bulkCopyMethod = fastestMethod.GetType().GetMyMethod(isAsync? "BulkMergeAsync" : "BulkMerge", 1);
|
||||||
}
|
}
|
||||||
|
private void _BulkMerge(DataTable dataTable, string[] whereColumns,string [] updateColumns, 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));
|
||||||
|
var propertyType = item.DataType;
|
||||||
|
if (!propertyType.IsClass() && propertyType != typeof(string) && propertyType != typeof(byte[]))
|
||||||
|
{
|
||||||
|
propertyType = typeof(Nullable<>).MakeGenericType(UtilMethods.GetUnderType(item.DataType));
|
||||||
|
}
|
||||||
|
builder.CreateProperty(item.ColumnName, propertyType, new SugarColumn()
|
||||||
|
{
|
||||||
|
IsPrimaryKey = isPrimaryKey,
|
||||||
|
IsIdentity = isIdentity && isPrimaryKey,
|
||||||
|
IsNullable = true,
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
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", 3, newValue.GetType(), typeof(string[]), typeof(string[]));
|
||||||
|
}
|
||||||
|
|
||||||
private async Task<int> _BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns)
|
private async Task<int> _BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns)
|
||||||
{
|
{
|
||||||
|
@@ -38,6 +38,8 @@ namespace SqlSugar
|
|||||||
int BulkMerge(List<T> datas);
|
int BulkMerge(List<T> datas);
|
||||||
int BulkMerge(DataTable dataTable, string[] whereColumns,bool isIdentity);
|
int BulkMerge(DataTable dataTable, string[] whereColumns,bool isIdentity);
|
||||||
Task<int> BulkMergeAsync(DataTable dataTable, string[] whereColumns, bool isIdentity);
|
Task<int> BulkMergeAsync(DataTable dataTable, string[] whereColumns, bool isIdentity);
|
||||||
|
int BulkMerge(DataTable dataTable, string[] whereColumns,string[] updateColumns, bool isIdentity);
|
||||||
|
Task<int> BulkMergeAsync(DataTable dataTable, string[] whereColumns, string[] updateColumns, 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);
|
||||||
|
Reference in New Issue
Block a user