Update db.Fastest

This commit is contained in:
sunkaixuna 2021-11-19 18:34:56 +08:00
parent 7050a293b2
commit d3b8ecaf4d
6 changed files with 52 additions and 36 deletions

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class FastBuilder
{
public SqlSugarProvider Context { get; set; }
public string UpdateSql { get; set; } = @"UPDATE TM
SET {0}
FROM {1} TM
INNER JOIN {2} TE ON {3} ";
public void CloseDb()
{
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
{
this.Context.Ado.Connection.Close();
}
}
public async Task<int> UpdateByTempAsync(string tableName, string tempName, string[] updateColumns, string[] whereColumns)
{
Check.ArgumentNullException(!updateColumns.Any(), "update columns count is 0");
Check.ArgumentNullException(!whereColumns.Any(), "where columns count is 0");
var sets = string.Join(",", updateColumns.Select(it => $"TM.{it}=TE.{it}"));
var wheres = string.Join(",", whereColumns.Select(it => $"TM.{it}=TE.{it}"));
string sql = string.Format(UpdateSql, sets, tableName, tempName, wheres);
return await this.Context.Ado.ExecuteCommandAsync(sql);
}
public async Task CreateTempAsync<T>(DataTable dt) where T : class, new()
{
await this.Context.UnionAll(
this.Context.Queryable<T>().Where(it => false).AS(dt.TableName),
this.Context.Queryable<T>().Where(it => false).AS(dt.TableName)).Select("top 1 * into #temp").ToListAsync();
dt.TableName = "#temp";
}
}
}

View File

@ -94,7 +94,7 @@ namespace SqlSugar
private async Task<int> _BulkCopy(List<T> datas) private async Task<int> _BulkCopy(List<T> datas)
{ {
DataTable dt = ToDdateTable(datas); DataTable dt = ToDdateTable(datas);
IFastBuilder buider = new SqlServerFastBuilder(); IFastBuilder buider =GetBuider();
buider.Context = context; buider.Context = context;
var result = await buider.ExecuteBulkCopyAsync(dt); var result = await buider.ExecuteBulkCopyAsync(dt);
return result; return result;

View File

@ -8,16 +8,9 @@ using System.Threading.Tasks;
namespace SqlSugar namespace SqlSugar
{ {
public class SqlServerFastBuilder: IFastBuilder
public class SqlServerFastBuilder:FastBuilder,IFastBuilder
{ {
public SqlSugarProvider Context { get; set; }
public string UpdateSql { get; set; } = @"UPDATE TM
SET {0}
FROM {1} TM
INNER JOIN {2} TE ON {3} ";
public async Task<int> ExecuteBulkCopyAsync(DataTable dt) public async Task<int> ExecuteBulkCopyAsync(DataTable dt)
{ {
@ -52,30 +45,6 @@ namespace SqlSugar
} }
return copy; return copy;
} }
public void CloseDb()
{
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
{
this.Context.Ado.Connection.Close();
}
}
public async Task<int> UpdateByTempAsync(string tableName, string tempName, string[] updateColumns, string[] whereColumns)
{
Check.ArgumentNullException(!updateColumns.Any(),"update columns count is 0");
Check.ArgumentNullException(!whereColumns.Any(), "where columns count is 0");
var sets = string.Join(",", updateColumns.Select(it=>$"TM.{it}=TE.{it}"));
var wheres = string.Join(",", whereColumns.Select(it => $"TM.{it}=TE.{it}"));
string sql = string.Format(UpdateSql,sets, tableName,tempName, wheres);
return await this.Context.Ado.ExecuteCommandAsync(sql);
}
public async Task CreateTempAsync<T>(DataTable dt) where T :class,new()
{
await this.Context.UnionAll(
this.Context.Queryable<T>().Where(it => false).AS(dt.TableName),
this.Context.Queryable<T>().Where(it => false).AS(dt.TableName)).Select("top 1 * into #temp").ToListAsync();
dt.TableName = "#temp";
}
} }
} }

View File

@ -88,9 +88,10 @@
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" /> <Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" />
<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\FastBuilder.cs" />
<Compile Include="Abstract\FastestProvider\FastestProvider.cs" /> <Compile Include="Abstract\FastestProvider\FastestProvider.cs" />
<Compile Include="Abstract\FastestProvider\_Private.cs" /> <Compile Include="Abstract\FastestProvider\Private.cs" />
<Compile Include="Abstract\FastestProvider\_Setting.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" />