mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Update db.Fastest
This commit is contained in:
parent
f8fa0d7708
commit
a0920bebdc
@ -32,8 +32,8 @@ namespace OrmTest
|
||||
|
||||
var insertObj = new Order() { Id = 1, Name = "order1", Price = 0 };
|
||||
var insertObjs = new List<Order> {
|
||||
new Order() { Id = 11, Name = "order11", Price=0 },
|
||||
new Order() { Id = 12, Name = "order12" , Price=0}
|
||||
new Order() { Id = 11, Name = "XX", Price=0 },
|
||||
new Order() { Id = 12, Name = "XX2" , Price=0}
|
||||
};
|
||||
|
||||
var x=db.Insertable(insertObjs).RemoveDataCache().IgnoreColumns(it=>it.CreateTime).UseParameter().ExecuteCommand();
|
||||
@ -156,6 +156,9 @@ namespace OrmTest
|
||||
dict.Add("CreateTime", DateTime.Now);
|
||||
dict.Add("Price", 1);
|
||||
db.Insertable(dict).AS("[Order]").ExecuteCommand();
|
||||
|
||||
|
||||
db.Fastest<Order>().BulkUpdate(insertObjs);
|
||||
Console.WriteLine("#### Insertable End ####");
|
||||
|
||||
}
|
||||
|
@ -3,19 +3,22 @@ using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class FastestProvider<T>:IFastest<T>
|
||||
public class FastestProvider<T>:IFastest<T> where T:class,new()
|
||||
{
|
||||
private SqlSugarProvider context;
|
||||
private ISugarQueryable<T> queryable;
|
||||
private string AsName { get; set; }
|
||||
private EntityInfo entityInfo { get; set; }
|
||||
public FastestProvider(SqlSugarProvider sqlSugarProvider)
|
||||
{
|
||||
this.context = sqlSugarProvider;
|
||||
this.queryable = this.context.Queryable<T>();
|
||||
entityInfo=this.context.EntityMaintenance.GetEntityInfo<T>();
|
||||
}
|
||||
#region Api
|
||||
public int BulkCopy(List<T> datas)
|
||||
{
|
||||
return BulkCopyAsync(datas).ConfigureAwait(true).GetAwaiter().GetResult();
|
||||
@ -28,11 +31,42 @@ namespace SqlSugar
|
||||
var result = await buider.ExecuteBulkCopyAsync(dt);
|
||||
return result;
|
||||
}
|
||||
public int BulkUpdate(List<T> datas)
|
||||
{
|
||||
return BulkUpdateAsync(datas).ConfigureAwait(true).GetAwaiter().GetResult();
|
||||
}
|
||||
public async Task<int> BulkUpdateAsync(List<T> datas)
|
||||
{
|
||||
var whereColumns=entityInfo.Columns.Where(it => it.IsPrimarykey).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();
|
||||
return await BulkUpdateAsync(datas,whereColumns,updateColumns);
|
||||
}
|
||||
public async Task<int> BulkUpdateAsync(List<T> datas,string [] whereColumns,string [] updateColumns)
|
||||
{
|
||||
var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection;
|
||||
this.context.CurrentConnectionConfig.IsAutoCloseConnection = false;
|
||||
DataTable dt = ToDdateTable(datas);
|
||||
IFastBuilder buider = new SqlServerFastBuilder();
|
||||
buider.Context = context;
|
||||
await buider.CreateTempAsync<T>(dt);
|
||||
await buider.ExecuteBulkCopyAsync(dt);
|
||||
//var queryTemp = this.context.Queryable<T>().AS(dt.TableName).ToList();//test
|
||||
var result =await buider.UpdateByTempAsync(GetTableName(),dt.TableName,updateColumns, whereColumns);
|
||||
this.context.DbMaintenance.DropTable(dt.TableName);
|
||||
this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto;
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Setting
|
||||
public IFastest<T> AS(string tableName)
|
||||
{
|
||||
this.AsName = tableName;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Helper
|
||||
private DataTable ToDdateTable(List<T> datas)
|
||||
{
|
||||
DataTable tempDataTable = ReflectionInoCore<DataTable>.GetInstance().GetOrCreate("BulkCopyAsync" + typeof(T).FullName, () => queryable.Where(it => false).ToDataTable());
|
||||
@ -41,8 +75,7 @@ namespace SqlSugar
|
||||
{
|
||||
dt.Columns.Add(item.ColumnName, item.DataType);
|
||||
}
|
||||
var entityInfo = this.context.EntityMaintenance.GetEntityInfo<T>();
|
||||
GetTableName(dt, entityInfo);
|
||||
dt.TableName = GetTableName();
|
||||
var columns = entityInfo.Columns;
|
||||
foreach (var item in datas)
|
||||
{
|
||||
@ -66,29 +99,27 @@ namespace SqlSugar
|
||||
|
||||
return dt;
|
||||
}
|
||||
|
||||
private void GetTableName(DataTable dt, EntityInfo entityInfo)
|
||||
private string GetTableName()
|
||||
{
|
||||
if (this.AsName.HasValue())
|
||||
{
|
||||
dt.TableName = queryable.SqlBuilder.GetTranslationTableName(AsName);
|
||||
return queryable.SqlBuilder.GetTranslationTableName(AsName);
|
||||
}
|
||||
else
|
||||
{
|
||||
dt.TableName = queryable.SqlBuilder.GetTranslationTableName(entityInfo.DbTableName);
|
||||
return queryable.SqlBuilder.GetTranslationTableName(entityInfo.DbTableName);
|
||||
}
|
||||
}
|
||||
private object ValueConverter(EntityColumnInfo columnInfo,object value)
|
||||
private object ValueConverter(EntityColumnInfo columnInfo, object value)
|
||||
{
|
||||
if (value == null)
|
||||
return value;
|
||||
if (value is DateTime&&(DateTime)value == DateTime.MinValue)
|
||||
if (value is DateTime && (DateTime)value == DateTime.MinValue)
|
||||
{
|
||||
value = Convert.ToDateTime("1900-01-01");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -1178,7 +1178,7 @@ namespace SqlSugar
|
||||
|
||||
#endregion
|
||||
#region
|
||||
public IFastest<T> Fastest<T>()
|
||||
public IFastest<T> Fastest<T>() where T:class,new()
|
||||
{
|
||||
return new FastestProvider<T>(this);
|
||||
}
|
||||
|
@ -10,7 +10,8 @@ namespace SqlSugar
|
||||
public interface IFastBuilder
|
||||
{
|
||||
SqlSugarProvider Context { get; set; }
|
||||
|
||||
Task<int> UpdateByTempAsync(string tableName,string tempName,string [] updateColumns,string[] whereColumns);
|
||||
Task<int> ExecuteBulkCopyAsync(DataTable dt);
|
||||
Task CreateTempAsync<T>(DataTable dt) where T : class, new();
|
||||
}
|
||||
}
|
||||
|
@ -10,5 +10,7 @@ namespace SqlSugar
|
||||
IFastest<T> AS(string tableName);
|
||||
int BulkCopy(List<T> datas);
|
||||
Task<int> BulkCopyAsync(List<T> datas);
|
||||
int BulkUpdate(List<T> datas);
|
||||
Task<int> BulkUpdateAsync(List<T> datas);
|
||||
}
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Fastest
|
||||
IFastest<T> Fastest<T>();
|
||||
IFastest<T> Fastest<T>() where T : class, new();
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
@ -13,6 +13,10 @@ namespace SqlSugar
|
||||
|
||||
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)
|
||||
{
|
||||
@ -55,5 +59,23 @@ namespace SqlSugar
|
||||
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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -566,7 +566,7 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region
|
||||
public IFastest<T> Fastest<T>()
|
||||
public IFastest<T> Fastest<T>() where T : class, new()
|
||||
{
|
||||
return this.Context.Fastest<T>();
|
||||
}
|
||||
|
@ -641,7 +641,7 @@ namespace SqlSugar
|
||||
return ScopedContext.IsAnyConnection(configId);
|
||||
}
|
||||
|
||||
public IFastest<T> Fastest<T>()
|
||||
public IFastest<T> Fastest<T>() where T : class, new()
|
||||
{
|
||||
return ScopedContext.Fastest<T>();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user