mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-11-08 02:14:53 +08:00
Update db.Fastest
This commit is contained in:
@@ -152,6 +152,7 @@ namespace SqlSugar
|
|||||||
column.EntityName = result.EntityName;
|
column.EntityName = result.EntityName;
|
||||||
column.PropertyName = property.Name;
|
column.PropertyName = property.Name;
|
||||||
column.PropertyInfo = property;
|
column.PropertyInfo = property;
|
||||||
|
column.UnderType = UtilMethods.GetUnderType(column.PropertyInfo.PropertyType);
|
||||||
if (sugarColumn.IsNullOrEmpty())
|
if (sugarColumn.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
column.DbColumnName = property.Name;
|
column.DbColumnName = property.Name;
|
||||||
|
|||||||
@@ -6,11 +6,12 @@ using System.Threading.Tasks;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public class FastestProvider<T>:IFastest<T> where T:class,new()
|
public partial class FastestProvider<T>:IFastest<T> where T:class,new()
|
||||||
{
|
{
|
||||||
private SqlSugarProvider context;
|
private SqlSugarProvider context;
|
||||||
private ISugarQueryable<T> queryable;
|
private ISugarQueryable<T> queryable;
|
||||||
private string AsName { get; set; }
|
private string AsName { get; set; }
|
||||||
|
private int Size { get; set; }
|
||||||
private EntityInfo entityInfo { get; set; }
|
private EntityInfo entityInfo { get; set; }
|
||||||
public FastestProvider(SqlSugarProvider sqlSugarProvider)
|
public FastestProvider(SqlSugarProvider sqlSugarProvider)
|
||||||
{
|
{
|
||||||
@@ -26,11 +27,19 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
public async Task<int> BulkCopyAsync(List<T> datas)
|
public async Task<int> BulkCopyAsync(List<T> datas)
|
||||||
{
|
{
|
||||||
DataTable dt = ToDdateTable(datas);
|
if (Size > 0)
|
||||||
IFastBuilder buider = new SqlServerFastBuilder();
|
{
|
||||||
buider.Context = context;
|
int resul=0;
|
||||||
var result = await buider.ExecuteBulkCopyAsync(dt);
|
await this.context.Utilities.PageEachAsync(datas, Size, async item =>
|
||||||
return result;
|
{
|
||||||
|
resul+= await _BulkCopy(item);
|
||||||
|
});
|
||||||
|
return resul;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return await _BulkCopy(datas);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -50,6 +59,26 @@ namespace SqlSugar
|
|||||||
return BulkUpdateAsync(datas,whereColumns,updateColumns).ConfigureAwait(true).GetAwaiter().GetResult();
|
return BulkUpdateAsync(datas,whereColumns,updateColumns).ConfigureAwait(true).GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
public async Task<int> BulkUpdateAsync(List<T> datas,string [] whereColumns,string [] updateColumns)
|
public async Task<int> BulkUpdateAsync(List<T> datas,string [] whereColumns,string [] updateColumns)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (Size > 0)
|
||||||
|
{
|
||||||
|
int resul = 0;
|
||||||
|
await this.context.Utilities.PageEachAsync(datas, Size, async item =>
|
||||||
|
{
|
||||||
|
resul += await _BulkUpdate(item, whereColumns, updateColumns);
|
||||||
|
});
|
||||||
|
return resul;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return await _BulkUpdate(datas, whereColumns, updateColumns);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Core
|
||||||
|
private async Task<int> _BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns)
|
||||||
{
|
{
|
||||||
var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection;
|
var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection;
|
||||||
this.context.CurrentConnectionConfig.IsAutoCloseConnection = false;
|
this.context.CurrentConnectionConfig.IsAutoCloseConnection = false;
|
||||||
@@ -64,107 +93,15 @@ namespace SqlSugar
|
|||||||
this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto;
|
this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
#endregion
|
private async Task<int> _BulkCopy(List<T> datas)
|
||||||
|
|
||||||
#region Setting
|
|
||||||
public IFastest<T> AS(string tableName)
|
|
||||||
{
|
{
|
||||||
this.AsName = tableName;
|
DataTable dt = ToDdateTable(datas);
|
||||||
return this;
|
IFastBuilder buider = new SqlServerFastBuilder();
|
||||||
|
buider.Context = context;
|
||||||
|
var result = await buider.ExecuteBulkCopyAsync(dt);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Helper
|
|
||||||
private SqlServerFastBuilder GetBuider()
|
|
||||||
{
|
|
||||||
switch (this.context.CurrentConnectionConfig.DbType)
|
|
||||||
{
|
|
||||||
case DbType.MySql:
|
|
||||||
break;
|
|
||||||
case DbType.SqlServer:
|
|
||||||
return new SqlServerFastBuilder();
|
|
||||||
case DbType.Sqlite:
|
|
||||||
break;
|
|
||||||
case DbType.Oracle:
|
|
||||||
break;
|
|
||||||
case DbType.PostgreSQL:
|
|
||||||
break;
|
|
||||||
case DbType.Dm:
|
|
||||||
break;
|
|
||||||
case DbType.Kdbndp:
|
|
||||||
break;
|
|
||||||
case DbType.Oscar:
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
throw new Exception(this.context.CurrentConnectionConfig.DbType + "开发中");
|
|
||||||
}
|
|
||||||
private DataTable ToDdateTable(List<T> datas)
|
|
||||||
{
|
|
||||||
DataTable tempDataTable = ReflectionInoCore<DataTable>.GetInstance().GetOrCreate("BulkCopyAsync" + typeof(T).FullName,
|
|
||||||
() =>
|
|
||||||
{
|
|
||||||
if (AsName == null)
|
|
||||||
{
|
|
||||||
return queryable.Where(it => false).ToDataTable();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return queryable.AS(AsName).Where(it => false).ToDataTable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
var dt = new DataTable();
|
|
||||||
foreach (DataColumn item in tempDataTable.Columns)
|
|
||||||
{
|
|
||||||
dt.Columns.Add(item.ColumnName, item.DataType);
|
|
||||||
}
|
|
||||||
dt.TableName = GetTableName();
|
|
||||||
var columns = entityInfo.Columns;
|
|
||||||
foreach (var item in datas)
|
|
||||||
{
|
|
||||||
var dr = dt.NewRow();
|
|
||||||
foreach (var column in columns)
|
|
||||||
{
|
|
||||||
if (column.IsIgnore || column.IsOnlyIgnoreInsert)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
var name = column.DbColumnName;
|
|
||||||
if (name == null)
|
|
||||||
{
|
|
||||||
name = column.PropertyName;
|
|
||||||
}
|
|
||||||
var value = ValueConverter(column, PropertyCallAdapterProvider<T>.GetInstance(column.PropertyName).InvokeGet(item));
|
|
||||||
dr[name] = value;
|
|
||||||
}
|
|
||||||
dt.Rows.Add(dr);
|
|
||||||
}
|
|
||||||
|
|
||||||
return dt;
|
|
||||||
}
|
|
||||||
private string GetTableName()
|
|
||||||
{
|
|
||||||
if (this.AsName.HasValue())
|
|
||||||
{
|
|
||||||
return queryable.SqlBuilder.GetTranslationTableName(AsName);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return queryable.SqlBuilder.GetTranslationTableName(entityInfo.DbTableName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private object ValueConverter(EntityColumnInfo columnInfo, object value)
|
|
||||||
{
|
|
||||||
if (value == null)
|
|
||||||
return value;
|
|
||||||
if (value is DateTime && (DateTime)value == DateTime.MinValue)
|
|
||||||
{
|
|
||||||
value = Convert.ToDateTime("1900-01-01");
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
103
Src/Asp.Net/SqlSugar/Abstract/FastestProvider/_Private.cs
Normal file
103
Src/Asp.Net/SqlSugar/Abstract/FastestProvider/_Private.cs
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
public partial class FastestProvider<T> : IFastest<T> where T : class, new()
|
||||||
|
{
|
||||||
|
private SqlServerFastBuilder GetBuider()
|
||||||
|
{
|
||||||
|
switch (this.context.CurrentConnectionConfig.DbType)
|
||||||
|
{
|
||||||
|
case DbType.MySql:
|
||||||
|
break;
|
||||||
|
case DbType.SqlServer:
|
||||||
|
return new SqlServerFastBuilder();
|
||||||
|
case DbType.Sqlite:
|
||||||
|
break;
|
||||||
|
case DbType.Oracle:
|
||||||
|
break;
|
||||||
|
case DbType.PostgreSQL:
|
||||||
|
break;
|
||||||
|
case DbType.Dm:
|
||||||
|
break;
|
||||||
|
case DbType.Kdbndp:
|
||||||
|
break;
|
||||||
|
case DbType.Oscar:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
throw new Exception(this.context.CurrentConnectionConfig.DbType + "开发中");
|
||||||
|
}
|
||||||
|
private DataTable ToDdateTable(List<T> datas)
|
||||||
|
{
|
||||||
|
DataTable tempDataTable = ReflectionInoCore<DataTable>.GetInstance().GetOrCreate("BulkCopyAsync" + typeof(T).FullName,
|
||||||
|
() =>
|
||||||
|
{
|
||||||
|
if (AsName == null)
|
||||||
|
{
|
||||||
|
return queryable.Where(it => false).ToDataTable();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return queryable.AS(AsName).Where(it => false).ToDataTable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
var dt = new DataTable();
|
||||||
|
foreach (DataColumn item in tempDataTable.Columns)
|
||||||
|
{
|
||||||
|
dt.Columns.Add(item.ColumnName, item.DataType);
|
||||||
|
}
|
||||||
|
dt.TableName = GetTableName();
|
||||||
|
var columns = entityInfo.Columns;
|
||||||
|
foreach (var item in datas)
|
||||||
|
{
|
||||||
|
var dr = dt.NewRow();
|
||||||
|
foreach (var column in columns)
|
||||||
|
{
|
||||||
|
if (column.IsIgnore || column.IsOnlyIgnoreInsert)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var name = column.DbColumnName;
|
||||||
|
if (name == null)
|
||||||
|
{
|
||||||
|
name = column.PropertyName;
|
||||||
|
}
|
||||||
|
var value = ValueConverter(column, PropertyCallAdapterProvider<T>.GetInstance(column.PropertyName).InvokeGet(item));
|
||||||
|
dr[name] = value;
|
||||||
|
}
|
||||||
|
dt.Rows.Add(dr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dt;
|
||||||
|
}
|
||||||
|
private string GetTableName()
|
||||||
|
{
|
||||||
|
if (this.AsName.HasValue())
|
||||||
|
{
|
||||||
|
return queryable.SqlBuilder.GetTranslationTableName(AsName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return queryable.SqlBuilder.GetTranslationTableName(entityInfo.DbTableName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private object ValueConverter(EntityColumnInfo columnInfo, object value)
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
return value;
|
||||||
|
if (value is DateTime && (DateTime)value == DateTime.MinValue)
|
||||||
|
{
|
||||||
|
value = Convert.ToDateTime("1900-01-01");
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
22
Src/Asp.Net/SqlSugar/Abstract/FastestProvider/_Setting.cs
Normal file
22
Src/Asp.Net/SqlSugar/Abstract/FastestProvider/_Setting.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
public partial class FastestProvider<T> : IFastest<T> where T : class, new()
|
||||||
|
{
|
||||||
|
public IFastest<T> AS(string tableName)
|
||||||
|
{
|
||||||
|
this.AsName = tableName;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public IFastest<T> PageSize(int size)
|
||||||
|
{
|
||||||
|
this.Size = size;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,5 +35,6 @@ namespace SqlSugar
|
|||||||
public string[] IndexGroupNameList { get; set; }
|
public string[] IndexGroupNameList { get; set; }
|
||||||
public string[] UIndexGroupNameList { get; set; }
|
public string[] UIndexGroupNameList { get; set; }
|
||||||
public bool IsArray { get; set; }
|
public bool IsArray { get; set; }
|
||||||
|
public Type UnderType { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,6 +89,8 @@
|
|||||||
<Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" />
|
<Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" />
|
||||||
<Compile Include="Abstract\ExpressionableProvider\Expressionable.cs" />
|
<Compile Include="Abstract\ExpressionableProvider\Expressionable.cs" />
|
||||||
<Compile Include="Abstract\FastestProvider\FastestProvider.cs" />
|
<Compile Include="Abstract\FastestProvider\FastestProvider.cs" />
|
||||||
|
<Compile Include="Abstract\FastestProvider\_Private.cs" />
|
||||||
|
<Compile Include="Abstract\FastestProvider\_Setting.cs" />
|
||||||
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
|
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
|
||||||
<Compile Include="Interface\IFastBuilder.cs" />
|
<Compile Include="Interface\IFastBuilder.cs" />
|
||||||
<Compile Include="Interface\IFastest.cs" />
|
<Compile Include="Interface\IFastest.cs" />
|
||||||
|
|||||||
Reference in New Issue
Block a user